/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager;

import br.com.deployment.entidade.selo.TipoSelo;
import br.com.deployment.servico.DetalheEntradaSeloServico;
import br.com.deployment.servico.SeloServico;
import br.com.deployment.servico.TipoSeloServico;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.ejb.EJB;
import javax.faces.bean.ViewScoped;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;

/**
 *
 * @author roniere
 */
@ManagedBean
@ViewScoped
public class ManagerEstoque implements Serializable{
    
    @EJB
    SeloServico seloDao;
    
    @EJB
    TipoSeloServico tipoSeloDao;
    
    @EJB
    DetalheEntradaSeloServico detalheEntradaSeloDao;
    
    private CartesianChartModel graEstoque;
    private CartesianChartModel graCancelado;
    private CartesianChartModel graSaida;
    private CartesianChartModel graSaidaMes;
    private CartesianChartModel graDistribuido;
    
    private long qtd;
    private CartesianChartModel categoryModel;
    private List<TipoSelo> tipoSelo;
    private long qtd1;
    private long qtdCancel;
    private Long qtdSairam;

    public ManagerEstoque() {
    }
    
    @PostConstruct
    public void init() throws ParseException{
        tipoSelo = new ArrayList<TipoSelo>();
        graSaida = new CartesianChartModel();
        tipoSelos();
        seloEstoque();
        seloUsados();
        seloCancelados();
        entradaSelo();
        createCategoryModel();
    }
    
    public void tipoSelos(){
        tipoSelo = tipoSeloDao.findAll();
    }
    public void seloEstoque() {
        
        qtd=0L;
        graEstoque = new CartesianChartModel();
        ChartSeries teste = new ChartSeries();
        
        Map<Object, Number> m;
        for (TipoSelo tipoSelo2 : tipoSelo) {
            Long n = seloDao.qtdSeloAll(tipoSelo2);
            
            m = new HashMap<Object, Number>();
            m.put(tipoSelo2.getNome(), n );
            teste.setLabel(tipoSelo2.getNome());
            teste.setData(m);
            
            if(qtd < n){
                qtd = n;
            }
            qtd = (qtd + (qtd / 3));
             graEstoque.addSeries(teste);
             teste = new ChartSeries();
        }
    }
    public void seloCancelados() {
        qtdCancel=0L;
        graCancelado = new CartesianChartModel();
        ChartSeries teste = new ChartSeries();
        
        Map<Object, Number> m;
        for (TipoSelo tipoSelo2 : tipoSelo) {
            Long n = seloDao.qtdSeloCancelado(tipoSelo2);
            
            m = new HashMap<Object, Number>();
            m.put(tipoSelo2.getNome(), n );
            teste.setLabel(tipoSelo2.getNome());
            teste.setData(m);
            
            if(qtdCancel < n){
                qtdCancel = n;
            }
            qtdCancel = (qtdCancel + (qtdCancel / 3));
             graCancelado.addSeries(teste);
             teste = new ChartSeries();
        }
    }

    public long getQtd1() {
        return qtd1;
    }

    public void setQtd1(long qtd1) {
        this.qtd1 = qtd1;
    }
    
    public void seloUsados() {
        qtd1=0L;
        graSaida = new CartesianChartModel();
        ChartSeries teste = new ChartSeries();
        
        Map<Object, Number> m;
        for (TipoSelo tipoSelo2 : tipoSelo) {
            Long n = seloDao.qtdSeloSairam(tipoSelo2);
            
            m = new HashMap<Object, Number>();
            m.put(tipoSelo2.getNome(), n );
            teste.setLabel(tipoSelo2.getNome());
            teste.setData(m);
            
            if(qtd1 < n){
                qtd1 = n;
            }
            qtd1 = (qtd1 + 10);
             graSaida.addSeries(teste);
             teste = new ChartSeries();
        }
    }
    
     private void createCategoryModel() throws ParseException {  
         qtdSairam = 0L;
        graSaidaMes = new CartesianChartModel(); 
        ChartSeries graMes = new ChartSeries();  
        graMes.setLabel("Selos");  
        List<String> lst = new ArrayList<String>();
        lst.add("JANEIRO");
        lst.add("FEVEREIRO");
        lst.add("MARÇO");
        lst.add("ABRIL");
        lst.add("MAIO");
        lst.add("JUNHO");
        lst.add("JULHO");
        lst.add("AGOSTO");
        lst.add("SETEMBRO");
        lst.add("OUTUBRO");
        lst.add("NOVEMBRO");
        lst.add("DEZEMBRO");
        
        Calendar d = new GregorianCalendar(2012, 0, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        
        Date dataIni = new Date();
        Date dataFim = new Date();
        
         for (String string : lst) {
              dataIni = d.getTime();
              d.add(Calendar.MONTH, 1);
              dataFim = d.getTime();
              Long n = seloDao.qtdSeloSairamMes(dataIni, dataFim);
              graMes.set(string, n);  
              d.setTime(dataFim);
              d.add(Calendar.DAY_OF_MONTH, 1);
              
              if(qtdSairam < n){
                  qtdSairam = n + 10;
              }
         }
         System.err.println(graMes.getData());
         
        graSaidaMes.addSeries(graMes);  
    }

    public Long getQtdSairam() {
        return qtdSairam;
    }

    public void setQtdSairam(Long qtdSairam) {
        this.qtdSairam = qtdSairam;
    }
     
     
    
    public void entradaSelo() throws ParseException{
        String s="01/07/2012";
        Date dta = new Date();
        Date dtaFim = new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
        dta = sdf.parse(s);
        dtaFim = sdf.parse("29/08/2012");
        detalheEntradaSeloDao.entradaSeloPorMes(dta, new Date());
        
        
        
        
    }

    public CartesianChartModel getGraSaidaMes() {
        return graSaidaMes;
    }

    public void setGraSaidaMes(CartesianChartModel graSaidaMes) {
        this.graSaidaMes = graSaidaMes;
    }

    public long getQtdCancel() {
        return qtdCancel;
    }

    public void setQtdCancel(long qtdCancel) {
        this.qtdCancel = qtdCancel;
    }
    
    

    public CartesianChartModel getCategoryModel() {
        return categoryModel;
    }

    public void setCategoryModel(CartesianChartModel categoryModel) {
        this.categoryModel = categoryModel;
    }

    public CartesianChartModel getGraCancelado() {
        return graCancelado;
    }

    public void setGraCancelado(CartesianChartModel graCancelado) {
        this.graCancelado = graCancelado;
    }

    public CartesianChartModel getGraDistribuido() {
        return graDistribuido;
    }

    public void setGraDistribuido(CartesianChartModel graDistribuido) {
        this.graDistribuido = graDistribuido;
    }

    public CartesianChartModel getGraEstoque() {
        return graEstoque;
    }

    public void setGraEstoque(CartesianChartModel graEstoque) {
        this.graEstoque = graEstoque;
    }

    public CartesianChartModel getGraSaida() {
        return graSaida;
    }

    public void setGraSaida(CartesianChartModel graSaida) {
        this.graSaida = graSaida;
    }

    public long getQtd() {
        return qtd;
    }

    public void setQtd(long qtd) {
        this.qtd = qtd;
    }
    
    
}
